﻿// Write a program that fills and prints a matrix of size (n, n) / Point C

using System;

class PrintMatrixPointC
{
    static void Main()
    {
        // read from console
        Console.Write("Enter Matrix size (n): ");
        int size = int.Parse(Console.ReadLine());

        int row = size - 1;
        int col = 0;
        int startDiagonalIndexRow = row;
        int startDiagonalIndexCol = col;
        int[,] matrix = new int[size, size];
        for (int i = 1; i <= size * size; i++)
        {
            matrix[row, col] = i;
            // store start index of next diagonal
            if (row == startDiagonalIndexRow && col == startDiagonalIndexCol)
            {
                if (row != 0)
                {
                    startDiagonalIndexRow = row - 1;
                }
                else
                {
                    startDiagonalIndexCol = col + 1;
                }
            }

            // fill matrix
            if (row == size - 1 || col == size - 1)
            {
                // start of next diagonal
                row = startDiagonalIndexRow;
                col = startDiagonalIndexCol;
            }
            else
            {
                row++;
                col++;
            }
        }

        // print matrix
        for (int r = 0; r < matrix.GetLength(0); r++)
        {
            for (int c = 0; c < matrix.GetLength(1); c++)
            {
                Console.Write("{0, 4}", matrix[r, c]);
                if (r != matrix.GetLength(0) - 1 || c != matrix.GetLength(1) - 1)
                {
                    Console.Write(", ");
                }
            }
            Console.WriteLine();
        }
    }
}